package com.vdongchina.miniapp.configure;

import com.vdongchina.miniapp.commons.RedissonRedisOps;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Copyright (C)
 * FileName: RedissionConfig
 * Author:   maokai
 * Date:     2021/7/8 9:41
 * Description:
 */
@Slf4j
@Configuration
public class RedissionConfig {
    @Autowired
    private RedisProperties redisProperties;

    /**
     * redissonClient 客户端
     */
    @Bean
    public RedissonClient redissonClient() {
        RedissonClient redissonClient;
        Config config = new Config();
        final String url = "redis://" + redisProperties.getHost() + ":" + redisProperties.getPort();
        config.useSingleServer()
                .setAddress(url)
                .setPassword(redisProperties.getPassword())
                .setDatabase(redisProperties.getDatabase());
        try {
            redissonClient = Redisson.create(config);
            return redissonClient;
        } catch (Exception e) {
            log.error("RedissonClient init redis url:[{}], Exception:", url, e);
            return null;
        }
    }

    /**
     * 初始化redisson操作对象
     */
    @Bean
    public RedissonRedisOps createRedissonClientOps() {
        return new RedissonRedisOps(redissonClient());
    }
}

